import { APollingTask, CacheData } from "../../PollingStation";

export class CacheDataPowerBank<T extends ICachePowerBankTask= ICachePowerBankTask> extends CacheData<T>
{

}

export class CachePowerBankTask<T = CacheDataPowerBank<ICachePowerBankTask>> extends APollingTask<T>
{
    // roomName!:string;

    Update(...args:any[]): void
    {
        // Memory.Debug[`${this.constructor.name}::Update ${Game.time}`] = this.code+":pb static update";
        // this.code = args[0];
        const room = Game.rooms[this.code];
        if(room)
        {
            const powerbanks = room.find(FIND_STRUCTURES,{filter: { structureType: STRUCTURE_POWER_BANK }}) as StructurePowerBank[];
            const cachedata:ICachePowerBankTask[]  = _.map(powerbanks,item=>{
                const ret:ICachePowerBankTask= {

                    id:item.id,
                    pos:item.pos,
                    power:item.power,
                    hits:item.hits,
                    decay:item.ticksToDecay+Game.time,

                } as ICachePowerBankTask
                return ret;
            });
            this.value = new CacheData<ICachePowerBankTask>(cachedata) as unknown as  T;

            //Memory.Debug[`${this.constructor.name}::查找PB ${Game.time}`] = JSON.stringify(this.value);
            if(cachedata.length>0)
            {
                this.lastupdate = Game.time + 1000;
            }
            else
            {
                this.lastupdate = Game.time + 50;
            }
        }
        else
        {
            throw new Error(`此房间没有视野 ${this.code} -time:${Game.time}`);
        }

    }

}
